/**
 * Ingenius-Group S.A.S
 *
 * Project: SAU
 * File: EditAcademicProgramPresenter.java
 * Date: 02/03/2011
 */

package com.ingenius.sau.client.presenter;

import java.util.ArrayList;
import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.shared.HandlerManager;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.HasWidgets;
import com.ingenius.sau.client.AppController;
import com.ingenius.sau.client.Constants;
import com.ingenius.sau.client.event.ListAcademicProgramsEvent;
import com.ingenius.sau.client.service.CoreServiceAsync;
import com.ingenius.sau.client.view.Display;
import com.ingenius.sau.shared.model.programs.AcademicProgram;

/**
 * Class <strong>EditAcademicProgramPresenter</strong>
 * 
 * @author Jesus David
 * @version 0.1
 */
public class EditAcademicProgramPresenter implements EditPresenter {

	public interface EditAcademicProgramDisplay extends Display {

		String getCode();

		void setCode(String code);

		String getName();

		void setName(String name);

		String getMission();

		void setMission(String mission);

		String getVision();

		void setVision(String vision);

		String getTitleGranted();

		void setTitleGranted(String titleGranted);

		String getModality();

		void setModality(String modality);

		void setModalities(List<String> modalities);

		Integer getDuration();

		void setDuration(Integer duration);

		void show();

		void hide();

	}

	private final HandlerManager eventBus;
	private final CoreServiceAsync rpcService;
	private final EditAcademicProgramDisplay display;
	private AcademicProgram program;

	public EditAcademicProgramPresenter(HandlerManager eventBus,
			CoreServiceAsync rpcService, EditAcademicProgramDisplay display,
			String programId) {

		this.rpcService = rpcService;
		this.eventBus = eventBus;
		this.display = display;
		display.setPresenter(this);
		loadModalities();

		if (programId == null) {
			program = new AcademicProgram();

		} else {
			loadBean(programId);
		}
	}

	private void loadModalities() {
		List<String> modalities = new ArrayList<String>();
		modalities.add(AcademicProgram.PRESENTIAL);
		modalities.add(AcademicProgram.NON_PRESENTIAL);
		display.setModalities(modalities);
	}

	private void loadBean(String programId) {
		AppController.setStatusMessage("Loading...");
		rpcService.getAcademicProgram(programId,
				new AsyncCallback<AcademicProgram>() {
					@Override
					public void onSuccess(AcademicProgram result) {
						if (result == null) {
							onFailure(new Throwable("Database error"));
							return;
						}
						program = result;
						display.setCode(program.getCode());
						display.setDuration(program.getDuration());
						display.setMission(program.getMission());
						display.setModality(program.getModality());
						display.setName(program.getName());
						display.setTitleGranted(program.getTitleGranted());
						display.setVision(program.getVision());
						hideStatusMessage();
					}

					@Override
					public void onFailure(Throwable caught) {
						GWT.log(Constants.MESSAGE_FAILED_GET
								+ AcademicProgram.class.getName(), caught);
						program = new AcademicProgram();
						// TODO i18n
						String failedMessage = "Failed to retrieve academic program";
						display.showInfo(failedMessage);
						hideStatusMessage();
					}

					void hideStatusMessage() {
						AppController.hideStatusMessage();
					}
				});
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.ingenius.sau.client.presenter.EditPresenter#doSaveOrUpdate()
	 */
	@Override
	public void doSaveOrUpdate() {
		program.setCode(display.getCode());
		program.setDuration(display.getDuration());
		program.setMission(display.getMission());
		program.setModality(display.getModality());
		program.setName(display.getName());
		program.setTitleGranted(display.getTitleGranted());
		program.setVision(display.getVision());

		// TODO i18n
		final String failedMessage = "Failed to save academic program";
		final String successMessage = "Academic program saved sucessfully";

		display.hide();
		AppController.setStatusMessage("Saving...");
		rpcService.saveOrUpdateAcademicProgram(program,
				new AsyncCallback<Boolean>() {
					@Override
					public void onSuccess(Boolean result) {
						if (result) {
							eventBus.fireEvent(new ListAcademicProgramsEvent());
							display.showInfo(successMessage);
							hideStatusMessage();

						} else {
							onFailure(new Throwable("Database error"));
						}
					}

					@Override
					public void onFailure(Throwable caught) {
						GWT.log(Constants.MESSAGE_FAILED_SAVE
								+ AcademicProgram.class.getName(), caught);
						display.showInfo(failedMessage);
						hideStatusMessage();
					}

					void hideStatusMessage() {
						AppController.hideStatusMessage();
					}
				});
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.ingenius.sau.client.presenter.Presenter#go(com.google.gwt.user.client
	 * .ui.HasWidgets)
	 */
	@Override
	public void go(HasWidgets container) {
		display.show();
	}

}
